/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "rep_general.h"
#include "rep_blinks.h"
#include "rep_title.h"
#include "GR_write_html.h"

void
write_body_hyperlinks_html_file(GraphFrame *gf,FILE *fp);
void
write_body_summary_html_file(GraphFrame *gf,FILE *fp);
void
write_body_pages_html_file(GraphFrame *gf,FILE *fp);
void
write_body_biggest_pages_html_file(GraphFrame *gf,FILE *fp, int npages);
void
write_body_references_pages_html_file(GraphFrame *gf,FILE *fp, int npages);

int
write_body_index_of_reports_summary_html_file(GraphFrame *gf,FILE *fp, 
					      char *prename, int nbigpages, 
					      int nrefpages);

int 
report_webgraph_summary(GraphFrame *gf, char *name_file)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Report of Web Site");
  write_body_summary_html_file(gf,fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

int 
report_hyperlinks(GraphFrame *gf, char *name_file)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Report of Hyperlinks");
  write_start_body_html_file(fp,"#ffffff");
  write_body_hyperlinks_html_file(gf,fp);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}


void
write_body_summary_html_file(GraphFrame *gf,FILE *fp)
{
  write_start_body_html_file(fp,"#ffffff");
  write_horizontal_rule_html_file(fp);
  fprintf(fp,GRWString);
  write_horizontal_rule_html_file(fp);
  write_body_pages_html_file(gf,fp);
  write_body_hyperlinks_html_file(gf,fp);
  write_end_body_html_file(fp);
}

void
write_body_hyperlinks_html_file(GraphFrame *gf,FILE *fp)
{
  LNode_type *nodes = get_list_vertices(gf);
  LNode_type *ptr = nodes->back;
  time_t now;

  int tlinks=0;
  int httpinthtmllinks = 0;
  int httpintdocslinks = 0;
  int httpextlinks = 0;
  int httpslinks = 0;
  int ftplinks = 0;
  int mailtolinks = 0;
  int filelinks = 0;
  int telnetlinks = 0;
  int gopherlinks = 0;
  int newslinks = 0;
  int otherlinks = 0;
  int blinks = 0;
  int ublinks = 0;
  int bobjlinks = 0;
  int ubobjlinks = 0;
  int objlinks = 0;
  int elinks = 0;
  int uelinks = 0;
  /* int framelinks = 0; */

  for(; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      LNode_type *peo, *ledges_out = get_edges_out(v);
      char vshape = get_shape(v);
      int vncode = vertex_http_code(v);
      
      if(vshape == TRIANGLE)
      {
	  if(is_vertex_object(v))
	     ubobjlinks++;
	  else if (vncode == 404)
	     ublinks++;
	  else
	     uelinks++;
      }

      for(peo = ledges_out->back ; peo != ledges_out ; peo = peo->back)
	{
	  PEDGE e = (PEDGE)peo->info;
	  PNODE u = get_to_vertex(e);
	  char *label = get_label_vertex(u);
	  char shape = get_shape(u);
	  int uncode = vertex_http_code(u);

	  if(get_edge_attribute(e) == 'z')
	  {
	    if(shape == TRIANGLE)
		bobjlinks++;
	    else
		objlinks++;
	    continue;
	  }

	  tlinks++;
	  if(shape == TRIANGLE && uncode == 404)
	      blinks++;
	  else if(shape == TRIANGLE && uncode != 404)
	      elinks++;
	  else if(shape == BOX)
	      httpinthtmllinks++;
	  else if(shape == CIRCULAR)
	      httpintdocslinks++;
	  else if(!strncasecmp(label,"http://",7))
	      httpextlinks++;
	  else if(!strncasecmp(label,"https://",8))
	      httpslinks++;
	  else if(!strncasecmp(label,"ftp://",6))
	      ftplinks++;
	  else if(!strncasecmp(label,"file:",5))
	      filelinks++;
	  else if(!strncasecmp(label,"mailto:",7))
	      mailtolinks++;
	  else if(!strncasecmp(label,"telnet:",7))
	      telnetlinks++;
	  else if(!strncasecmp(label,"gopher:",7))
	      gopherlinks++;
	  else if(!strncasecmp(label,"news:",5))
	      newslinks++;
	  else
	      otherlinks++;
	}
    }

  now = time(NULL);
  write_header_html_file(fp,1,"Report of Hyperlinks");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_start_table_html_file(fp, 2);

  write_row_data(fp, "Total Number of Hyperlinks", tlinks);
  write_row_data(fp, "HTTP to internal HTML docs", httpinthtmllinks);
  write_row_data(fp, "HTTP to internal Non HTML Docs", httpintdocslinks);
  write_row_data(fp, "HTTP to external Docs", httpextlinks);
  if(objlinks > 0)
      write_row_data(fp, "HTTP to objects", objlinks);
  write_row_data(fp, "HTTPS", httpslinks);
  write_row_data(fp, "FTP", ftplinks);
  write_row_data(fp, "MAILTO", mailtolinks);
  write_row_data(fp, "FILE", filelinks);
  write_row_data(fp, "TELNET", telnetlinks);
  write_row_data(fp, "GOPHER", gopherlinks);
  write_row_data(fp, "NEWS", newslinks);
  write_row_data(fp, "OTHER", otherlinks);
  write_row_data(fp, "Broken Links", blinks);
  write_row_data(fp, "Unique Broken Links", ublinks);
  write_row_data(fp, "Error Links", elinks);
  write_row_data(fp, "Unique Error Links", uelinks);
  if(objlinks > 0)
  {
    write_row_data(fp, "Broken Links to Objects", bobjlinks);
    write_row_data(fp, "Unique Broken Links to Objects", ubobjlinks);
  }
      
  write_end_table_html_file(fp);
}

int 
report_pages(GraphFrame *gf, char *name_file)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Report of Pages");
  write_start_body_html_file(fp,"#ffffff");
  write_body_pages_html_file(gf,fp);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
increment_count_mime_list(LNode_type *lmime,const char *smime)
{
  const char *svalue = find_value_assoc_list(lmime,smime);
  if(svalue)
  {
    int ncount = atoi(svalue);
    char nvalue[30];
    ncount++;
    sprintf(nvalue,"%d", ncount);	
    replace_object_assoc_list(lmime,smime,nvalue);
  }
  else
  {
    add_object_assoc_list(lmime,smime,"1");
  }
}

void
increment_size_mime_list(LNode_type *lmime,const char *smime,int vsize)
{
  const char *svalue = find_value_assoc_list(lmime,smime);
  if(svalue)
  {
    int tsize = atoi(svalue);
    char nvalue[30];
    tsize+=vsize;
    sprintf(nvalue,"%d", tsize);	
    replace_object_assoc_list(lmime,smime,nvalue);
  }
  else
  {
    char nvalue[30];
    sprintf(nvalue,"%d", vsize);	
    add_object_assoc_list(lmime,smime,nvalue);
  }
}

void
write_body_pages_html_file(GraphFrame *gf,FILE *fp)
{
  LNode_type *nodes = get_list_vertices(gf);
  LNode_type *ptr = nodes->back;
  LNode_type *ptr2 = NULL;
  time_t now;
  LNode_type *lmime = init_assoc_list();
  LNode_type *lmime2 = init_assoc_list();
  LNode_type *lomime = init_assoc_list();
  LNode_type *lomime2 = init_assoc_list();
  double total_size_html = 0;
  double total_size_non_html = 0;
  double total_size_obj = 0;
  int num_html_pages =0;
  int num_non_html_pages =0;
  int num_obj = 0;

  for(; ptr != nodes; ptr=ptr->back)
    {
      PNODE v = (PNODE)ptr->info;
      const char *smime = get_attribute(v, "mime");
      char *sweight = get_weight_vertex(v);
      int vsize = sweight ? atoi(sweight) : 0;

      if(is_vertex_object(v))
      {
	if(smime)
	{
	  increment_count_mime_list(lomime,smime);
	  increment_size_mime_list(lomime2,smime,vsize);
	  num_obj++;
	  total_size_obj +=vsize;
	}
	continue;
      }

      if(smime)
      {
	increment_count_mime_list(lmime,smime);
	increment_size_mime_list(lmime2,smime,vsize);
	if(!strcmp(smime,"text/html"))
	{
	  num_html_pages++;
	  total_size_html+=vsize;
	}
	else
	{
	  num_non_html_pages++;
	  total_size_non_html+=vsize;
	}
      }
    }

  now = time(NULL);
  write_header_html_file(fp,1,"Report of Pages");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_start_table_html_file(fp, 2);
  {
    PNODE root = get_root_vertex(gf);
    char *sroot = root ? get_label_vertex(root) : NULL;
    char svalue[30];
    int nvertex = get_count_vertex(gf);
    if(sroot)
      write_row_string(fp,"Initial URL", sroot);
    else
      write_row_string(fp,"Initial URL", "");
   sprintf(svalue,"%dK",(int)((total_size_html+total_size_non_html)/1000.00));
   write_row_string(fp,"Total Size",svalue);
   sprintf(svalue,"%dK",(int)(total_size_html/1000.00));
   write_row_string(fp,"Total Size HTML Docs",svalue);
   sprintf(svalue,"%dK",(int)(total_size_non_html/1000.00));
   write_row_string(fp,"Total Size Non HTML Docs",svalue);
   if(num_obj > 0)
   {
     sprintf(svalue,"%dK",(int)(total_size_obj/1000.00));
     write_row_string(fp,"Total Size Objects",svalue);
   }
   write_row_data(fp,"Total Number of Docs", nvertex);
   write_row_data(fp,"Total Number of HTML Docs", num_html_pages);
   write_row_data(fp,"Total Number of Non HTML Docs", num_non_html_pages);
   write_row_data(fp,"Total Number of External Docs", 
		  nvertex - num_html_pages - num_non_html_pages - num_obj);
   if(num_obj > 0)
       write_row_data(fp,"Total Number of Objects", num_obj);

  }
  write_end_table_html_file(fp);
  write_header_html_file(fp,2,"File Types");
  write_start_table_html_file(fp, 3);
  write_header_table_string2(fp,"MIME","NUMBER","SIZE");
  ptr2 = lmime2->back;
  for(ptr = lmime->back; ptr != lmime; ptr = ptr->back)
  {
    char *aname= NULL, *avalue = NULL; 
    char *aname2 = NULL, *avalue2 = NULL;
    if(!get_node_info_assoc_list(ptr,&aname,&avalue))
    {
      if(!get_node_info_assoc_list(ptr2, &aname2, &avalue2))
      {
	if(aname && avalue && avalue2)
	{
	    char svalue2[30];
	    int nvalue2 = 0;
	    nvalue2 = atoi(avalue2);
	    sprintf(svalue2,"%d K", nvalue2/1000);
	    write_row_string2(fp,aname,avalue,svalue2);
	}
      }
    }
    ptr2 = ptr2->back;
  }
  write_end_table_html_file(fp);
  if(num_obj > 0) {
  write_header_html_file(fp,2,"Object Types");
  write_start_table_html_file(fp, 3);
  write_header_table_string2(fp,"MIME","NUMBER","SIZE");
  ptr2 = lomime2->back;
  for(ptr = lomime->back; ptr != lomime; ptr = ptr->back)
  {
    char *aname= NULL, *avalue = NULL; 
    char *aname2 = NULL, *avalue2 = NULL;
    if(!get_node_info_assoc_list(ptr,&aname,&avalue))
    {
      if(!get_node_info_assoc_list(ptr2, &aname2, &avalue2))
      {
	if(aname && avalue && avalue2)
	{
	    char svalue2[30];
	    int nvalue2 = 0;
	    nvalue2 = atoi(avalue2);
	    sprintf(svalue2,"%d K", nvalue2/1000);
	    write_row_string2(fp,aname,avalue,svalue2);
	}
      }
    }
    ptr2 = ptr2->back;
  } 
  write_end_table_html_file(fp); }
  delete_assoc_list(lmime); lmime = NULL;
  delete_assoc_list(lmime2); lmime2 = NULL;
  delete_assoc_list(lomime); lomime = NULL;
  delete_assoc_list(lomime2); lomime2 = NULL;
}

int 
report_biggest_pages(GraphFrame *gf, char *name_file,int npages)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Report of Largest Pages");
  write_start_body_html_file(fp,"#ffffff");
  write_body_biggest_pages_html_file(gf,fp,npages);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_biggest_pages_html_file(GraphFrame *gf,FILE *fp, int npages)
{
  LNode_type *nodes = get_list_vertices(gf);
  LNode_type *ptr = nodes->back;
  time_t now;
  PNODE *varray = NULL;
  PNODE *varray2 = NULL;
  PNODE *varray3 = NULL;
  int nhtml=0,nnhtml=0, nobj=0;
  char ntitle[100];
  int i=0,j=0,k=0;
  int tnpages=0;

  for(; ptr != nodes; ptr=ptr->back)
  {
    PNODE v = (PNODE)ptr->info;
    const char *smime = get_attribute(v, "mime");
    if(smime)
    {
      if(is_vertex_object(v))
	nobj++;
      else if(!strcmp(smime,"text/html"))
	nhtml++;
      else
	nnhtml++;
    }
  }

  varray = nhtml ? (PNODE *)calloc(nhtml,sizeof(PNODE)) : NULL;

  if(!varray && nhtml > 0) {
      fprintf(stderr,"Out of Memory write_body_pages_html_file\n");
      abort();
  }
  varray2 = nnhtml ? (PNODE *)calloc(nnhtml,sizeof(PNODE)) : NULL;

  if(!varray2 && nnhtml > 0) {
      fprintf(stderr,"Out of Memory write_body_pages_html_file\n");
      abort();
  }

  varray3 = nobj ? (PNODE *)calloc(nobj,sizeof(PNODE)) : NULL;

  if(!varray3 && nobj > 0) {
      fprintf(stderr,"Out of Memory write_body_pages_html_file\n");
      abort();
  }

  for(ptr = nodes->back; ptr != nodes; ptr=ptr->back)
  {
    PNODE v = (PNODE)ptr->info;
    const char *smime = get_attribute(v, "mime");
    if(smime)
    {
      if(is_vertex_object(v))
	varray3[k++] = v;
      else if(!strcmp(smime,"text/html"))
	varray[i++] = v;
      else
	varray2[j++] = v;
    }
  }

  if(nhtml > 0) {
  if(npages > nhtml)
      tnpages = nhtml;
  else
      tnpages = npages;

  now = time(NULL);
  write_header_html_file(fp,1,"Report of Largest Pages");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RBPWString);
  write_horizontal_rule_html_file(fp);
  sprintf(ntitle,"%d Largest HTML Pages", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_table_html_file(fp, 2);

  for(i = 1; i<=tnpages ; i++)
  {
    PNODE v = (PNODE)randomized_select((GPTR *)varray,i-1,nhtml-1,1,
			  is_vertex_weight_lower,is_vertex_weight_bigger);
    if(v)
    {
	char *vlabel = get_label_vertex(v);
	char *vsize = get_weight_vertex(v);
	write_row_string(fp,vlabel,vsize);
    }
  }
  write_end_table_html_file(fp); }

  if(nnhtml > 0) {
  if(npages > nnhtml)
      tnpages = nnhtml;
  else
      tnpages = npages;

  sprintf(ntitle,"%d Largest Non HTML Pages", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_table_html_file(fp, 2);

  for(i = 1; i<=tnpages ; i++)
  {
    PNODE v = (PNODE)randomized_select((GPTR *)varray2,i-1,nnhtml-1,1,
			  is_vertex_weight_lower,is_vertex_weight_bigger);
    if(v)
    {
	char *vlabel = get_label_vertex(v);
	char *vsize = get_weight_vertex(v);
	write_row_string(fp,vlabel,vsize);
    }
  }
  write_end_table_html_file(fp); }

  if(nobj > 0) {
  if(npages > nobj)
      tnpages = nobj;
  else
      tnpages = npages;

  sprintf(ntitle,"%d Largest Objects", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_table_html_file(fp, 2);

  for(i = 1; i<=tnpages ; i++)
  {
    PNODE v = (PNODE)randomized_select((GPTR *)varray3,i-1,nobj-1,1,
			  is_vertex_weight_lower,is_vertex_weight_bigger);
    if(v)
    {
	char *vlabel = get_label_vertex(v);
	char *vsize = get_weight_vertex(v);
	write_row_string(fp,vlabel,vsize);
    }
  }
  write_end_table_html_file(fp); }

  free(varray); varray = NULL;
  free(varray2); varray2 = NULL;
  free(varray3); varray3 = NULL;

}


int 
report_references_pages(GraphFrame *gf, char *name_file,int npages)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Report of Highest References to Pages");
  write_start_body_html_file(fp,"#ffffff");
  write_body_references_pages_html_file(gf,fp,npages);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_references_pages_html_file(GraphFrame *gf,FILE *fp, int npages)
{
  LNode_type *nodes = get_list_vertices(gf);
  LNode_type *ptr = nodes->back;

  time_t now;

  PNODE *varray = NULL; /* HTML ref in */
  PNODE *varray2 = NULL; /* HTML ref out */
  PNODE *varray3 = NULL; /* NON HTML ref in */

  int nhtml=0, nnhtml=0, nobj=0;
  char ntitle[100];
  int i=0,j=0,k=0;
  int tnpages=0;

  for(; ptr != nodes; ptr=ptr->back)
  {
    PNODE v = (PNODE)ptr->info;
    const char *smime = get_attribute(v, "mime");
    if(smime)
    {
      if(is_vertex_object(v))
	nobj++;
      else if(!strcmp(smime,"text/html"))
	nhtml++;
      else
	nnhtml++;
    }
  }

  varray = nhtml ? (PNODE *)calloc(nhtml,sizeof(PNODE)) : NULL;

  if(!varray && nhtml > 0) {
      fprintf(stderr,"Out of Memory write_body_references_pages_html_file\n");
      abort();
  }
  varray2 = nnhtml ? (PNODE *)calloc(nnhtml,sizeof(PNODE)) : NULL;

  if(!varray2 && nnhtml > 0) {
      fprintf(stderr,"Out of Memory write_body_references_pages_html_file\n");
      abort();
  }

  varray3 = nobj ? (PNODE *)calloc(nobj,sizeof(PNODE)) : NULL;

  if(!varray3 && nobj > 0) {
      fprintf(stderr,"Out of Memory write_body_references_pages_html_file\n");
      abort();
  }

  for(ptr = nodes->back; ptr != nodes; ptr=ptr->back)
  {
    PNODE v = (PNODE)ptr->info;
    const char *smime = get_attribute(v, "mime");
    if(smime)
    {
      if(is_vertex_object(v))
	varray3[k++] = v;
      else if(!strcmp(smime,"text/html"))
	varray[i++] = v;
      else
	varray2[j++] = v;
    }
  }

  if(nhtml > 0) {
  if(npages > nhtml)
      tnpages = nhtml;
  else
      tnpages = npages;

  now = time(NULL);
  write_header_html_file(fp,1,"Report of Pages with Highest References");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RPHRString);
  write_horizontal_rule_html_file(fp);
  sprintf(ntitle,"%d HTML Pages with Highest Number of Hyperlinks", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_table_html_file(fp, 6);
  write_header_table_string5(fp,"URL","Total Hyperlinks",
			     "Hyperlinks to HTML Pages", 
			     "Hyperlinks to non HTML Pages", 
			     "Hyperlinks to External Pages", 
			     "No HTTP Hyperlinks");

  for(i = 1; i<=tnpages ; i++)
  {
    PNODE v = (PNODE)randomized_select((GPTR *)varray,i-1,nhtml-1,1,
				       is_vertex_num_hyperlinks_lower,
				       is_vertex_num_hyperlinks_bigger);
    if(v)
    {
	char *vlabel = get_label_vertex(v);
	int neo = num_hyperlinks_out(v);
	int nein = num_hyperlinks_to_html(v);
	int neinh = num_hyperlinks_to_nhtml(v);
	int neex = num_hyperlinks_to_ext(v);
	int neoth = neo - nein - neinh - neex;
	write_row_data5(fp,vlabel,neo,nein,neinh,neex,neoth);
    }
  }
  write_end_table_html_file(fp); }

  if(nhtml > 0 && nobj > 0) {
  if(npages > nhtml)
      tnpages = nhtml;
  else
      tnpages = npages;

  write_break_html_file(fp);
  sprintf(ntitle,"%d HTML Pages with Highest Number of Objects", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_table_html_file(fp, 2);

  for(i = 1; i<=tnpages ; i++)
  {
    PNODE v = (PNODE)randomized_select((GPTR *)varray,i-1,nhtml-1,1,
				       is_vertex_num_ref_objects_lower,
				       is_vertex_num_ref_objects_bigger);
    if(v)
    {
	char *vlabel = get_label_vertex(v);
	int neo = num_ref_objects_out(v);
	write_row_data(fp,vlabel,neo);
    }
  }
  write_end_table_html_file(fp); }

  if(nhtml > 0) {
  if(npages > nhtml)
      tnpages = nhtml;
  else
      tnpages = npages;

  write_break_html_file(fp);
  sprintf(ntitle,"%d HTML Pages with Highest References", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_table_html_file(fp, 2);

  for(i = 1; i<=tnpages ; i++)
  {
    PNODE v = (PNODE)randomized_select((GPTR *)varray,i-1,nhtml-1,1,
				       is_vertex_num_edges_in_lower,
				       is_vertex_num_edges_in_bigger);
    if(v)
    {
	char *vlabel = get_label_vertex(v);
	int nei = count_llist(get_edges_in(v));
	write_row_data(fp,vlabel,nei);
    }
  }
  write_end_table_html_file(fp); }

  if(nnhtml > 0) {
  if(npages > nnhtml)
      tnpages = nnhtml;
  else
      tnpages = npages;

  sprintf(ntitle,"%d Non HTML Pages with Highest References ", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_table_html_file(fp, 2);

  for(i = 1; i<=tnpages ; i++)
  {
    PNODE v = (PNODE)randomized_select((GPTR *)varray2,i-1,nnhtml-1,1,
				       is_vertex_num_edges_in_lower,
				       is_vertex_num_edges_in_bigger);
    if(v)
    {
	char *vlabel = get_label_vertex(v);
	int nei = count_llist(get_edges_in(v));
	write_row_data(fp,vlabel,nei);
    }
  }
  write_end_table_html_file(fp); }

  if(nobj > 0) {
  if(npages > nobj)
      tnpages = nobj;
  else
      tnpages = npages;

  sprintf(ntitle,"%d Objects with Highest References", tnpages);
  write_header_html_file(fp,2,ntitle);
  write_start_table_html_file(fp, 2);

  for(i = 1; i<=tnpages ; i++)
  {
    PNODE v = (PNODE)randomized_select((GPTR *)varray3,i-1,nobj-1,1,
				       is_vertex_num_edges_in_lower,
				       is_vertex_num_edges_in_bigger);
    if(v)
    {
	char *vlabel = get_label_vertex(v);
	int nei = count_llist(get_edges_in(v));
	write_row_data(fp,vlabel,nei);
    }
  }
  write_end_table_html_file(fp); }

  free(varray); varray = NULL;
  free(varray2); varray2 = NULL;
  free(varray3); varray3 = NULL;

}

int
report_index_of_reports(GraphFrame *gf, char *prename, int nbigpages,
			int nrefpages)
{
  char name_file[100];
  int status = 0;
  FILE *fp = NULL;
  sprintf(name_file,"%s_report_index.html", prename);
  fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Index of Reports of Web Site");
  status = write_body_index_of_reports_summary_html_file(gf,fp, prename, 
							 nbigpages,nrefpages);
  write_end_html_file(fp);
  fclose(fp);
  return status;
}

int
write_body_index_of_reports_summary_html_file(GraphFrame *gf,FILE *fp, 
					      char *prename, int nbigpages, 
					      int nrefpages)
{
  char name_file[100];
  char title[500];
  int status = 0;
  int nobj = count_llist(get_list_objects(gf));
  PNODE root = get_root_vertex(gf);
  char *sroot = root ? get_label_vertex(root) : NULL;
  time_t now;
  now = time(NULL);
  write_start_body_html_file(fp,"#ffffff");
  if(sroot)
      sprintf(title,"Index of Reports of Web Site %s", sroot);
  else
      sprintf(title,"Index of Reports of Web Site");
  write_header_html_file(fp,1,title);
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_start_ul_html_file(fp);

  sprintf(name_file,"%sgen.html",prename);
  status = report_webgraph_summary(gf,name_file);
  if(status != 0) return -1;
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,name_file);
  fprintf(fp,"General Report of Web site");
  write_end_a_href_html_file(fp);
  write_break_html_file(fp);
  fprintf(fp,GRWString);

  sprintf(name_file,"%spbr.html",prename);
  status = report_ord_pages_broken_links(gf,name_file);
  if(status != 0) return -1;
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,name_file);
  fprintf(fp,"Report of Pages with Broken Links");
  write_end_a_href_html_file(fp);
  fprintf(fp,"(%d Pages)\n", num_html_pages_with_error_http_code(gf,404));
  write_break_html_file(fp);
  fprintf(fp,RPBLString);

  sprintf(name_file,"%slbr.html",prename);
  status = report_ord_broken_links(gf,name_file);
  if(status != 0) return -1;
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,name_file);
  fprintf(fp,"Report of List of Broken Links");
  write_end_a_href_html_file(fp);
  fprintf(fp,"(%d Broken Links)\n",num_vertices_http_code(gf,404));
  write_break_html_file(fp);
  fprintf(fp,RLBLString);

  if(nobj > 0) {
  sprintf(name_file,"%sbobj.html",prename);
  status = report_ord_broken_objects(gf,name_file);
  if(status != 0) return -1;
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,name_file);
  fprintf(fp,"Report of List of Broken Objects");
  write_end_a_href_html_file(fp); 
  fprintf(fp,"(%d Broken Objects)\n", num_objects_http_code(gf,404)); 
  write_break_html_file(fp);
  fprintf(fp,RLBOString);}

  sprintf(name_file,"%stit.html",prename);
  status = report_ord_pages_without_title(gf,name_file);
  if(status != 0) return -1;
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,name_file);
  fprintf(fp,"Report of Pages without title");
  write_end_a_href_html_file(fp);
  fprintf(fp,"(%d Pages)\n", num_html_pages_without_title(gf)); 
  write_break_html_file(fp);
  fprintf(fp,RPWTString);
      
  sprintf(name_file,"%sorp.html",prename);
  status = report_ord_orphan_pages(gf,name_file);
  if(status != 0) return -1;
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,name_file);
  fprintf(fp,"Report of Orphan Pages");
  write_end_a_href_html_file(fp);
  fprintf(fp,"(%d Pages)\n", num_orphan_html_pages(gf)); 
  write_break_html_file(fp);
  fprintf(fp,ROPString);

  if(nobj > 0) {
  sprintf(name_file,"%spimg.html",prename);
  status = report_ord_pages_images_no_alt(gf,name_file);
  if(status != 0) return -1;
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,name_file);
  fprintf(fp,"Report of Pages with Images without ALT attribute");
  write_end_a_href_html_file(fp); 
  fprintf(fp,"(%d Pages)\n", num_html_pages_with_images_without_alt(gf)); 
  write_break_html_file(fp);
  fprintf(fp,RPIAString);}
      

  sprintf(name_file,"%sbig.html",prename);
  status = report_biggest_pages(gf,name_file,nbigpages);
  if(status != 0) return -1;
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,name_file);
  fprintf(fp,"Report of Largest Pages of Web site");
  write_end_a_href_html_file(fp);
  fprintf(fp,"(%d Pages)\n", nbigpages);
  write_break_html_file(fp);
  fprintf(fp,RBPWString);

  sprintf(name_file,"%sref.html",prename);
  status = report_references_pages(gf,name_file,nrefpages);
  if(status != 0) return -1;
  write_item_html_file(fp);
  write_start_a_href_html_file(fp,name_file);
  fprintf(fp,"Report of Pages with Highest References");
  write_end_a_href_html_file(fp);
  fprintf(fp,"(%d Pages)\n", nrefpages);
  write_break_html_file(fp);
  fprintf(fp,RPHRString );

  write_end_ul_html_file(fp);
  write_end_body_html_file(fp);

  return status;
}



